import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/components/login.vue'
import Home from '@/components/home.vue'
import Welcome from '@/components/welcome.vue'
import Users from '@/components/users.vue'
import Rights from '@/components/rights.vue'
import Roles from '@/components/roles.vue'
import Categories from '@/components/categories.vue'
import Params from '@/components/params.vue'
import Goods from '@/components/goods.vue'
import AddGoods from '@/components/addGoods.vue'
import Orders from '@/components/orders.vue'
import Reports from '@/components/reports.vue'


Vue.use(VueRouter)


const router = new VueRouter({
  routes:[
    // 重定向路由
    { path: '/', redirect: '/login'},
    // 登录页面
    { path: '/login', component:Login},
    //home
    { 
      path: '/home',
      component: Home,
      redirect:'/welcome',
      children:[
        { path:'/welcome', component: Welcome },
        { path:'/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goods/addgoods', component: AddGoods },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})

// 给router挂载路由守卫
router.beforeEach((to, from,next) => {
  // to 将要到哪个页面  from 从哪个页面来 next 直接放行
  // 判断要跳转的页面是不是login
  if(to.path === '/login') return next();
  // 判断是否有token值
  const tokenStr = window.sessionStorage.getItem("token");
  //没登陆跳转到登录页面
  if(!tokenStr) return next("/login");
  // 否则直接放行
  next();
})

export default router
